package com.liming.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.liming.model.Goods;
import com.liming.utils.DataSourceUtils;

/**
 * 
 * @author chengw
 * @version 20190628
 */
public class Goodsdao {
	
	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	
	/**
	 * 
	 * @param id
	 * @return 查询到的商品，如果查询值为空，返回空商品
	 * @throws SQLException 
	 */
	public Goods getGoodsByid(int id) throws SQLException {
		return qr.query("select * from goods where goodsid = ?",new BeanHandler<>(Goods.class), id);

	}
	
	public List<Goods> getGoodsByName(String name) throws SQLException {	
		return qr.query("select * from goods where goodsname REGEXP ?",new BeanListHandler<>(Goods.class), name);
	}

	public List<Goods> getHotGoods() throws SQLException {
		// TODO Auto-generated method stub
		return qr.query("select * from goods where ishot = 1",new BeanListHandler<>(Goods.class));
	}

	public List<Goods> getGoodsByPrice(int min, int max) throws SQLException {
		// TODO Auto-generated method stub
		return qr.query("select * from goods where price >= ? and price <= ?",new BeanListHandler<>(Goods.class), min,max);
	}

	public List<Goods> getGoodsByType(int typeid) throws SQLException {
		// TODO Auto-generated method stub
		return qr.query("select * from goods where typeid = ?",new BeanListHandler<>(Goods.class), typeid);
	}

}
